import java.util.TimeZone.DisplayNames;

import com.surelution.projects.domain.Role
import com.surelution.projects.domain.User;
import com.surelution.projects.domain.UserRole;
import com.surelution.projects.echo.domain.PredefineMessage;
import com.surelution.projects.echo.domain.Status;

class BootStrap {

	def springSecurityService

    def init = { servletContext ->
		def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true)
		def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
		
		def adminUser = User.findByUsername("admin")?: 
					new User(username : "admin", 
						displayName : "常总",
						password : "admin", 
						enabled :true).save(failOnError : true)
		if(!adminUser.authorities.contains(adminRole)) {
			UserRole.create(adminUser, adminRole)
		}
		
		def postScript = PredefineMessage.findByCode("POST_SCRIPT")
		if(!postScript) {
			postScript = new PredefineMessage(code:"POST_SCRIPT", content:"")
			postScript.save(flush:true)
		}
		
		def footerScript = PredefineMessage.findByCode("FOOT_MESSAGE")
		if(!footerScript) {
			footerScript = new PredefineMessage(code:"FOOT_MESSAGE", content:"")
			footerScript.save(flush:true)
		}
		
		def newStatus = Status.findByKey(Status.NEW)?: new Status(key : Status.NEW, label : "未读").save()
		def readStatus = Status.findByKey(Status.READ)?: new Status(key : Status.READ, label : "已读").save()
		def openStatus = Status.findByKey(Status.OPEN)?: new Status(key : Status.OPEN, label : "开放").save()
    }
    def destroy = {
    }
}
